﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.BIM;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HYSpace
{
	public partial class FrmCheckWall : System.Windows.Forms.Form
	{
		public FrmCheckWall(UIDocument document, bool beSure)
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitializeComponent();
			this.uiDoc = document;
			this.uiApp = document.Application;
			this.isSetLayer = beSure;
		}

		private void FrmCheckWall_Load(object sender, EventArgs e)
		{
			this.listRA = WallInfoOperate.GetAllRegionArea();
			this.InitialSurfaceInfo(this.listRA, this.GetProCityName(this.uiDoc.Document));
		}

		private string GetProCityName(Document doc)
		{
			string text = "";
			SiteLocation siteLocation = doc.SiteLocation;
			double latitude = siteLocation.Latitude;
			double longitude = siteLocation.Longitude;
			if (siteLocation.PlaceName != "")
			{
				IEnumerator<RegionArea> enumerator = this.listRA.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						RegionArea regionArea = enumerator.Current;
                        if (this.DoubleEquals(UnitConvert.CovertToAPI(Convert.ToDouble(regionArea.Latitude), (Autodesk.Revit.DB.DisplayUnitType)15), latitude) && this.DoubleEquals(UnitConvert.CovertToAPI(Convert.ToDouble(regionArea.Longitude), (Autodesk.Revit.DB.DisplayUnitType)15), longitude))
						{
							text = regionArea.Name;
						}
					}
					goto IL_C6;
				}
			}
			string text2 = doc.ProjectInformation.GetParameter(BuiltInParameter.PROJECT_ADDRESS).AsString();
			if (!string.IsNullOrEmpty(text2) && text2 != "请在此处输入地址")
			{
				text = text2;
			}
			IL_C6:
			if (string.IsNullOrEmpty(text))
			{
				text = "北京";
			}
			return text;
		}

		private City GetCity(UIApplication uiApplicate, double latitude, double longitude)
		{
			foreach (object obj in uiApplicate.Application.Cities)
			{
				City city = (City)obj;
				if (this.DoubleEquals(city.Latitude, latitude) && this.DoubleEquals(city.Longitude, longitude))
				{
					return city;
				}
			}
			return null;
		}

		public bool DoubleEquals(double x, double y)
		{
			double num = 0.017453292519943295;
			return Math.Abs(x / num - y / num) < 1.5;
		}

		public double GetWallHeatRatio(Document doc, int shape)
		{
			IList<RegionArea> allRegionArea = WallInfoOperate.GetAllRegionArea();
			double num = -1.0;
			double num2 = -1.0;
			string proCityName = this.GetProCityName(doc);
			foreach (RegionArea regionArea in allRegionArea)
			{
				if (regionArea.Name == proCityName || proCityName.Contains(regionArea.Name))
				{
					num = regionArea.Shape3;
					num2 = regionArea.Shape4;
				}
			}
			if (num < 0.0)
			{
				num = allRegionArea[2].Shape3;
				num2 = allRegionArea[2].Shape4;
			}
			double result;
			if (shape != 3)
			{
				if (shape != 4)
				{
					result = num;
				}
				else
				{
					result = num2;
				}
			}
			else
			{
				result = num;
			}
			return result;
		}

		private void InitialSurfaceInfo(IList<RegionArea> listRA, string pointName)
		{
			int num = 0;
			foreach (RegionArea regionArea in listRA)
			{
				if (regionArea.Name == pointName || pointName.Contains(regionArea.Name))
				{
					num = Convert.ToInt32(regionArea.RTypeId);
				}
			}
			if (num == 0)
			{
				num = Convert.ToInt32(listRA[3].RTypeId);
			}
			this.cmbRegion.SelectedIndex = num;
			this.SetTextContent();
		}

		private void rdoShape3_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoShape3.Checked)
			{
				this.rdoShape4.Checked = false;
			}
			else
			{
				this.rdoShape4.Checked = true;
			}
			this.SetTextContent();
		}

		private void rdoShape4_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoShape4.Checked)
			{
				this.rdoShape3.Checked = false;
			}
			else
			{
				this.rdoShape3.Checked = true;
			}
			this.SetTextContent();
		}

		private void SetTextContent()
		{
			foreach (RegionArea regionArea in this.listRA)
			{
				if (regionArea.RTypeId == this.cmbRegion.SelectedIndex)
				{
					if (this.rdoShape3.Checked)
					{
						this.txtCoefficient.Text = regionArea.Shape3.ToString();
						break;
					}
					if (this.rdoShape4.Checked)
					{
						this.txtCoefficient.Text = regionArea.Shape4.ToString();
						break;
					}
				}
			}
		}

		private void cmbRegion_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SetTextContent();
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnCheck_Click(object sender, EventArgs e)
		{
			double num = Convert.ToDouble(this.txtCoefficient.Text);
			if (num <= 0.0)
			{
				num = 0.1;
			}
			if (this.isSetLayer)
			{
				this.standardValue = num;
				base.DialogResult = DialogResult.Yes;
				return;
			}
			Transaction transaction = new Transaction(this.uiDoc.Document, "检查墙体传热系数");
			transaction.Start();
			WallInfoOperate.SetWallTypeDefaultName(this.uiDoc.Document, 1.125);
			IList<Wall> list = new List<Wall>();
			foreach (Element element in YJKElementFilter.GetElementByCategory(this.uiDoc.Document, typeof(Wall), BuiltInCategory.OST_Walls))
			{
				list.Add(element as Wall);
			}
			if (list.Count <= 0)
			{
				YJKMessageBox.Show("没有要检查的墙体！");
				transaction.RollBack();
				return;
			}
			List<WallType> termWallType = WallInfoOperate.GetTermWallType(list, 0, 1);
			List<WallType> list2 = new List<WallType>();
			IList<HeatRatio> allWallHeatRatio = WallInfoOperate.GetAllWallHeatRatio();
			foreach (WallType wallType in termWallType)
			{
				foreach (HeatRatio heatRatio in allWallHeatRatio)
				{
					if (!(wallType.Name != heatRatio.WallName) && heatRatio.KValue > num)
					{
						list2.Add(wallType);
					}
				}
			}
			Selection selection = this.uiDoc.Selection;
			foreach (WallType wallType2 in list2)
			{
				WallInfoOperate.BrightTermWall(list, wallType2, selection);
			}
			YJKMessageBox.Show("检查完成，亮线墙体\n为传热系数不符合规范墙体");
			this.uiDoc.Document.Regenerate();
			transaction.Commit();
		}

		private UIDocument uiDoc;

		private UIApplication uiApp;

		public double standardValue;

		private bool isSetLayer;

		private IList<RegionArea> listRA = new List<RegionArea>();
	}
}
